const { ccclass, property } = cc._decorator;
@ccclass
export default abstract class CountDownControl extends cc.Component {
    public static TIME_INTERVAL: number = 1;
    /**
     *  计时结束回调
     */
    @property([cc.Component.EventHandler])
    public CountDownEvent: cc.Component.EventHandler[] = [];

    /**
     *  每秒结束回调
     */
    @property([cc.Component.EventHandler])
    public SecondEvent: cc.Component.EventHandler[] = [];

    /**
     *  每毫秒结束回调
     */
    @property([cc.Component.EventHandler])
    public MillisecondEvent: cc.Component.EventHandler[] = [];

    protected mFullTime: number = 0;
    public set FullTime(value: number) {
        this.mFullTime = value;
        this.mBookTime = this.mFullTime;
    }
    public get FullTime(): number {
        return this.mFullTime;
    }

    protected mBookSecond: number = 0;
    protected mBookTime: number = 0;

    protected mIsPause: boolean = false;
    public set Pause(value: boolean) {
        this.mIsPause = value;
    }
    public get Pause(): boolean {
        return this.mIsPause;
    }

    /**
     * 启动
     * @param time 时间戳 
     */
    public open(time: number): void {
        this.mFullTime = time * (1 / CountDownControl.TIME_INTERVAL);
        this.mBookTime = this.mFullTime;
        this.mBookSecond = 0;
        this.mIsPause = false;

        this.refreshUI();
        this.schedule(this.updateCountDown, CountDownControl.TIME_INTERVAL);
    }

    /**
     * 关闭
     */
    public close(): void {
        this.mIsPause = true;
        this.mFullTime = 0;
        this.mBookTime = 0;
        this.refreshUI();
        this.unschedule(this.updateCountDown);
    }

    /**
     * 提速
     * @param multi 提速倍数  
     */
    public changeCountDownDroopSpeed(multi: number): void {
        this.unschedule(this.updateCountDown);
        this.schedule(this.updateCountDown, CountDownControl.TIME_INTERVAL * multi, 1);
    }

    /**
     * 增加倍数
     * @param multi 倍数
     */
    public changeCoutDownMax(multi: number): void {
        this.mFullTime = this.mFullTime * multi;
        this.mBookTime = this.mBookTime * multi;
    }

    /**
     * 获取进度
     * @returns 进度比例 
     */
    public getProgress(): number {
        if (this.mFullTime <= 0)
            return 0;
        let book = this.mBookTime;
        let full = this.mFullTime;
        return book / full;
    }

    /**
     * 补满
     * @param time 补充值 
     */
    public beFull(time: number): void {
        this.mBookTime = time;
        this.mFullTime = time;
        this.refreshUI();
    }

    /**
     * 是否结束
     * @returns 
     */
    public isOver(): boolean {
        return this.mBookTime <= 0;
    }

    protected updateCountDown(): void {
        if (this.mIsPause)
            return;
        this.mBookTime--;
        this.mBookSecond++;
        if (this.mBookSecond >= 100) {
            this.SecondEvent.forEach(element => {
                element.emit([""]);
            });
        }
        if (this.mBookTime <= 0) {
            this.CountDownEvent.forEach(element => {
                element.emit([""]);
            });
            this.close();
        }
        else {
            this.refreshUI();
            this.MillisecondEvent.forEach(element => {
                element.emit([""]);
            });
        }
    }

    public abstract refreshUI(): void;
}